<!--
 * @description  :物流档案维护
 * @descriptionDetail:物流档案维护
 * @copyright    : 浙江烟草
 * @author       : dw
 * @create       : 2022-10-09 22:21:21
-->
<template>
  <view class="page">
    <!-- #ifdef H5-ONLY -->
    <theme-navbar title="物流档案维护"></theme-navbar>
    <!-- #endif -->
    <view class="content content-bg ifn-content" style="background-color: white">
      <u-form
        class="u-form-m"
        :label-style="labelStyle"
        :model="baseInfo"
        :error-type="['border-bottom', 'message',]"
        label-width="auto"
        label-position="top"
        ref="uForm"
      >
        <u-form-item label="登记单位" :required="true">
          <u-input :disabled="true" :custom-style="pointerStyle" placeholder="请选择" v-model="baseInfo.orgName"></u-input>
        </u-form-item>
        <u-form-item label="物流公司名称" prop="name" :required="true">
          <u-input v-model="baseInfo.name" :disabled="isDisabled" placeholder="请输入"></u-input>
        </u-form-item>
        <u-form-item label="法定代表人(负责人)">
          <u-input v-model="baseInfo.corporation" placeholder="请输入"></u-input>
        </u-form-item>
        <u-form-item label="服务电话" prop="tel">
          <u-input v-model="baseInfo.tel" type="number" placeholder="请输入"></u-input>
        </u-form-item>
        <u-form-item label="网址">
          <u-input v-model="baseInfo.webset" placeholder="请输入"></u-input>
        </u-form-item>
        <u-form-item label="承运路线">
          <u-input v-model="baseInfo.way" placeholder="请输入"></u-input>
        </u-form-item>
        <u-form-item label="管辖单位" right-icon="arrow-down-fill" :right-icon-style="iconStyle">
          <u-input
            :disabled="true" :custom-style="pointerStyle" placeholder="请选择" v-model="baseInfo.managerOrgName"
            @click="orgSelectShow = true"
          ></u-input>
        </u-form-item>
        <u-form-item
          label="城市" :border-bottom="false" prop="provCityCountyName"
          :required="true"
        >
          <view style="display: flex; flex-direction: column; width: 100%">
            <u-input
              type="select"
              :disabled="true"
              placeholder="所在地选择"
							:custom-style="pointerStyle"
              v-model="baseInfo.provCityCountyName"
              @click=" (nativeNameSelectShow = !isDisabled)"
            ></u-input>
            <u-line :color="$theme.u_border_color" style="margin-top: 20rpx"></u-line>
          </view>
        </u-form-item>
        <u-form-item prop="address">
          <u-input
            :border="true" placeholder="地址" :disabled="isDisabled"
            v-model="baseInfo.address"
          ></u-input>
        </u-form-item>
        <u-form-item label="网点名称" prop="point" :required="true">
          <u-input v-model="baseInfo.point" :disabled="isDisabled" placeholder="请输入"></u-input>
        </u-form-item>
        <u-form-item label="经纬度">
          <!-- placeGisX placeGisY -->
          <u-input
            placeholder="请选择" :disabled="true" :custom-style="pointerStyle" :needValid="!isDisabled"
            v-model="baseInfo.lonlat"
          ></u-input>
          <u-icon
            name="map-fill" :color="$theme.u_type_primary" size="50"
            @click="getLocation()"
          ></u-icon>
        </u-form-item>
        <u-form-item label="公司简介" :border-bottom="false">
          <u-input v-model="baseInfo.brief" type="textarea" placeholder="请输入"></u-input>
        </u-form-item>
      </u-form>
    </view>
    <view class="u-form">
      <u-gap :height="$theme.cm_section_top_gap_height" :bg-color="$theme.cm_gap_color"></u-gap>
      <fsection title="图片信息" :customStyle="customStyle"></fsection>
    </view>
    <view class="u-form-m pl-10">
      <m-upload
        :uploadParam="fileUploadParam" upApiUrl="uploadBase64ForApp" :fileList="fileList"
        @on-list-change="changeUpload"
      ></m-upload>
    </view>
    <view class="submit-btns bottom-btns">
      <u-button class="del" type="primary" @click="submit">
        保&nbsp;&nbsp;&nbsp;&nbsp;存
      </u-button>
    </view>
    <u-popup mode="bottom" height="80%" v-model="nativeNameSelectShow">
      <area-select :choiceAll="true" @choose="nativeNameSelectShowComfir"></area-select>
    </u-popup>
    <!-- 组织机构选择 -->
    <u-popup mode="bottom" height="80%" v-model="orgSelectShow">
      <org-select
        :cleanParentOrg="true" :isHybrid="true" limitNodeType="all"
        treeType="zm-all" @choose="handlerChooseOrg"
      ></org-select>
    </u-popup>
  </view>
</template>

<script>
import areaSelect from '@/components/area-select.vue';
import OrgSelect from '@/components/org-select.vue';
import intelligenceservice from '@/service/intelligence/intelligence.service';
import commonService from '@/service/common/common.service.js';
import fsection from '@/components/form-section.vue';
import MUpload from '@/components/m-upload.vue';
import { uuid, } from '@/common/util.js';
export default {
  components: {
    areaSelect,
    OrgSelect,
    fsection,
    MUpload,
  },
  data() {
    return {
      rules: {
        name: [
          {
            required: true,
            message: '请输入物流公司名称',
            // 可以单个或者同时写两个触发验证方式
            trigger: ['change', 'blur',],
          },
        ],
        point: [
          {
            required: true,
            message: '请输入网点',
            // 可以单个或者同时写两个触发验证方式
            trigger: ['change', 'blur',],
          },
        ],
        isActiveName: [
          {
            required: true,
            message: '请选择状态',
            // 可以单个或者同时写两个触发验证方式
            trigger: ['change', 'blur',],
          },
        ],
        provCityCountyName: [
          {
            required: true,
            message: '请选择所在城市',
            // 可以单个或者同时写两个触发验证方式
            trigger: ['change', 'blur',],
          },
        ],
        address: [
          {
            required: true,
            message: '请输入地址',
            // 可以单个或者同时写两个触发验证方式
            trigger: ['change', 'blur',],
          },
        ],
        tel: [
          {
            validator: (rule, value, callback) => {
              if (this.$u.test.isEmpty(value)) {
                return true;
              }
              {
                return this.$u.test.mobile(value) || this.$u.test.tel(value);
              }
            },
            message: '电话格式不正确!',
            // 可以单个或者同时写两个触发验证方式
            trigger: ['change', 'blur',],
          },
        ],
      },
      baseInfo: {
        address: '',
        brief: '',
        corporation: '',
        historyUuid: '',
        menuCode: '',
        name: '',
        orgUuid: '',
        orgName: '',
        point: '',
        provCityCounty: [],
        provCityCountyName: '',
        provCode: '',
        cityCode: '',
        countyCode: '',
        tel: '',
        way: '',
        webset: '',
        lonlat: '',
        longitude: '',
        latitude: '',
      },
      iconStyle: {
        color: '#c0c4cc',
      },
      customStyle: {
        padding: '20rpx 32rpx',
      },
      labelStyle: {
        fontWeight: 400,
        color: this.$theme.u_main_color,
        fontSize: '.75rem',
      },
      from: 'add',
      show: false,
      orgSelectShow: false,
      nativeNameSelectShow: false,
      user: this.$storage.get(this.$rp.common.USER),
      isDisabled: false,
      fileList: [],
      fileUploadParam: {
        fiGroupCode: this.$FILE_GROUP_TYPE.k_transport_attr,
        bizId: '',
      },
			// h5端部分浏览器input设置disabled后点击事件失效 2023-10-27 fsx start
			pointerStyle: {
			  marginRight: "5px",
				pointerEvents: "none !important"
			},
			// h5端部分浏览器input设置disabled后点击事件失效 2023-10-27 fsx end
    };
  },
  onLoad(opt) {
    this.from = opt.from;
    if (this.from == 'edit') {
      this.isDisabled = false;
      this.baseInfo = this.getRouteParam();
      console.log('ddddd', this.$store.state.areaObj[this.baseInfo.countyCode]);
      if (!this.$u.test.isEmpty(this.baseInfo.longitude)) {
        this.baseInfo.lonlat = `${this.baseInfo.longitude},${this.baseInfo.latitude}`;
      } else {
        this.baseInfo.lonlat = '';
      }
      this.baseInfo.provCityCountyName =
					this.$store.state.areaObj[this.baseInfo.countyCode] ||
					this.$store.state.areaObj[this.baseInfo.cityCode] ||
					this.$store.state.areaObj[this.baseInfo.provCode] ||
					'';
      // if (this.$u.test.isEmpty(this.baseInfo.cityCode) && this.$u.test.isEmpty(this.baseInfo.countyCode)) {
      //   this.baseInfo.provCityCountyName = this.baseInfo.provCodeName;
      // } else if (this.$u.test.isEmpty(this.baseInfo.countyCode)) {
      //   this.baseInfo.provCityCountyName = this.baseInfo.provCodeName + '/' + this.baseInfo.cityCodeName;
      // } else if (!this.$u.test.isEmpty(this.baseInfo.countyCode)) {
      //   this.baseInfo.provCityCountyName = this.baseInfo.provCodeName + '/' + this.baseInfo.cityCodeName + '/' + this.baseInfo.countyCodeName;
      // } else if (!this.$u.test.isEmpty(this.baseInfo.provCode)) {
      //   this.baseInfo.provCityCountyName = '';
      // }
      this.fileUploadParam.bizId = this.baseInfo.uuid;
      let params = {
        bizId: this.baseInfo.uuid,
        group: this.$FILE_GROUP_TYPE.k_transport_attr,
        menuCode: null,
      };
      intelligenceservice.findFilesByBizIdAndGroup(params).then((res) => {
        if (res.success) {
          this.fileList = res.data;
        }
      });
    } else {
      this.baseInfo.uuid = uuid();
      this.fileUploadParam.bizId = this.baseInfo.uuid;
      if (this.user.orgLevel === 1) {
        this.baseInfo.orgName = this.user.provOrgName;
        this.baseInfo.orgUuid = this.user.provOrgUuid;
      } else {
        this.baseInfo.orgName = this.user.townOrgName;
        this.baseInfo.orgUuid = this.user.townOrgUuid;
      }
    }
  },
  onReady() {
    this.$refs.uForm.setRules(this.rules);
  },
  mounted() {
    // this.init();
  },
  methods: {
    handlerChooseOrg(orgs) {
      this.baseInfo.managerOrgUuid = orgs[0].id;
      this.baseInfo.managerOrgName = orgs[0].name;
      this.orgSelectShow = false;
    },
    nativeNameSelectShowComfir(areas) {
      if (this.$u.test.isEmpty(areas[0].admdivCode) && this.$u.test.isEmpty(areas[0].countryCode)) {
        this.baseInfo.provCityCountyName = areas[0].proName;
      } else if (this.$u.test.isEmpty(areas[0].countryCode)) {
        this.baseInfo.provCityCountyName = areas[0].proName + '/' + areas[0].admdivName;
      } else if (!this.$u.test.isEmpty(areas[0].countryCode)) {
        this.baseInfo.provCityCountyName = areas[0].proName + '/' + areas[0].admdivName + '/' + areas[0].countryName;
      }
      this.baseInfo.provCode = areas[0].proCode;
      this.baseInfo.cityCode = areas[0].admdivCode;
      this.baseInfo.countyCode = areas[0].countryCode;
      this.baseInfo.provCityCounty = areas;
      this.nativeNameSelectShow = false;
    },
    // 上传/删除图片事件
    changeUpload(list, key) {
      if (list.length > 0) {
        this.fileList = [...list,];
      } else {
        this.fileList = [];
      }
    },
    updateBackState(param) {
      if (param.pos) {
        //  定位
        // 回显经纬度
        this.baseInfo.longitude = param.pos.longitude;
        this.baseInfo.latitude = param.pos.latitude;
        this.baseInfo.lonlat = `${param.pos.longitude},${param.pos.latitude}`;
        let addressName = param.pos.regeocode.addressComponent;
        let area = commonService.getLocalName(addressName.province, addressName.city, addressName.district);
        if (this.$modal.isMuniCity(area.proCode)) {
          this.baseInfo.provCityCountyName = area.admdivName + '/' + area.areaName;
        } else {
          this.baseInfo.provCityCountyName = area.proName + '/' + area.admdivName + '/' + area.areaName;
        }
        this.baseInfo.provCode = area.proCode;
        this.baseInfo.cityCode = area.admdivCode;
        this.baseInfo.countyCode = area.countryCode;
      }
      this.$forceUpdate();
    },
    getLocation() {
      if (this.isDisabled) return;
      this.setRouteParam({ disable: false, step: 'start', });
      this.$u.route('pages/commonPage/modifyLocationMap');
    },
    submit() {
      this.$refs.uForm.validate((valid) => {
        if (valid) {
          let params = this.baseInfo;
          this.$modal.showLoading('保存中...');
          intelligenceservice
            .savelogisticsInfo(params)
            .then((res) => {
              this.$modal.hideLoading();
              if (res.success) {
                this.baseInfo = res.data;
                this.backPrev({
                  paramKey: 'refresh',
                  param: true,
                });
              } else {
                this.$u.toast(res.msg);
              }
            })
            .catch((err) => {
              this.$modal.hideLoading();
              console.log(err);
              this.$u.toast('保存失败');
            });
        } else {
          console.log('验证失败');
        }
      });
    },
    close() {
      this.$u.route('pages/intelligence/intelligenceTransportList');
    },
  },
};
</script>

<style scoped lang="scss">
	@import './add.scss';
</style>
